package i2.application.fmw.dao;

import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;

import org.dbunit.IDatabaseTester;
import org.dbunit.JdbcDatabaseTester;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.xml.FlatDtdDataSet;
import org.dbunit.dataset.xml.FlatXmlWriter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/applicationContextTest.xml"})
@Transactional
public class DatasetGenerator {

	@Test
	public  void main() throws Exception {
		// database connection
//		Class driverClass = Class.forName("org.hsqldb.jdbcDriver");
//		Connection jdbcConnection = DriverManager.getConnection("jdbc:hsqldb:mem:checklist", "sa", "");
//		IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
		IDatabaseTester dataBaseTester = new JdbcDatabaseTester("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/checklist", "root", "admin");
		
		IDatabaseConnection dbUnitConnection = dataBaseTester.getConnection();
		// write DTD file
		FlatDtdDataSet.write(dbUnitConnection.createDataSet(), new FileOutputStream("test.dtd"));
		
		FlatXmlWriter datasetWriter = new FlatXmlWriter(new FileOutputStream("dataset.xml")); 
		datasetWriter.setDocType("test.dtd"); 
		datasetWriter.write(dbUnitConnection.createDataSet());
		
//		IDataSet dataSet = connection.createDataSet();
//        Writer out = new OutputStreamWriter(new FileOutputStream("myFile.dtd"));
//        FlatDtdWriter datasetWriter = new FlatDtdWriter(out);
//        datasetWriter.setContentModel(FlatDtdWriter.CHOICE);
//        // You could also use the sequence model which is the default
//        // datasetWriter.setContentModel(FlatDtdWriter.SEQUENCE);
//        datasetWriter.write(dataSet);
     
	}
}
